home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 80 / CD Actual 80 Julio-Agosto 2003.iso / Linux / LinuxGazette / lg / issue83 / misc / sandeep / Tracer.c < prev   
Encoding:
C/C++ Source or Header  |  2002-10-01  |  2.2 KB  |  77 lines

  1. #include <stdio.h>
  2. #include <sys/ptrace.h>
  3. #include <linux/user.h>
  4. #include <signal.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8. #include <sys/types.h>
  9. #include <sys/wait.h>
  10.  
  11. void injected_shellcode();
  12. char *shellcode;
  13. char *mesg = 
  14.     "\x31\xc0\xb0\x04\xeb\x0f\x31\xdb\x43\x59"
  15.     "\x31\xd2\xb2\x0d\xcd\x80\xa1\x78\x56\x34"
  16.     "\x12\xe8\xec\xff\xff\xff\x09\x4f\x68\x2c\x09"
  17.     "\x43\x61\x75\x67\x68\x74\x09\x20\x0a\x0a\x21";  
  18.     /* Prints The Message */
  19.  
  20. int Tracer(pid_t pid)
  21. {
  22.     int     error, ptr, begin, i = 0;
  23.     struct     user_regs_struct data;   /* Structure to store the Registers */
  24.     
  25.     if ((error = ptrace(PTRACE_ATTACH, pid, NULL, NULL))){        
  26.         perror("Attach");
  27.         exit(1);
  28.     }
  29.     waitpid(pid, NULL, 0);              /* Wait for the process to stop */
  30.     
  31.     if ((error = ptrace(PTRACE_GETREGS, pid, NULL, &data)))
  32.         perror("Getregs");
  33.     printf("%%eip : 0x%.8lx\n", data.eip);         /* Print the contents */
  34.     printf("%%esp : 0x%.8lx\n", data.esp);         /* of registers       */
  35.     
  36.     ptr = begin = data.esp - 512;          /* Get the location to which 
  37.                                  we    have to write */
  38.     printf("Inserting shellcode into %.8lx\n", (long)begin);
  39.     data.eip = (long) begin;               /* Change the Pointer */
  40.     
  41.     ptrace(PTRACE_SETREGS, pid, NULL, &data);       /* Set the Registers */    
  42.     
  43.     while (i < strlen(shellcode)) {            /* Insert the code   */    
  44.         ptrace(PTRACE_POKETEXT, pid, ptr,    /* to the process    */    
  45.                (int) *(int *) (shellcode + i));    /* image         */
  46.         i += 4;
  47.         ptr += 4;
  48.     }
  49.     ptrace(PTRACE_DETACH, pid, NULL, NULL);        /* Detach the Process*/ 
  50.                             /* Don't Forget         */
  51.     return 0;                    
  52. }
  53.  
  54. int main(int argc, char **argv)
  55. {
  56.     pid_t pid;                    /* Process Id        */    
  57.     
  58.     if (argc < 2) 
  59.         return puts("Usage:./catch pid");
  60.  
  61.     pid = atoi(argv[1]);
  62.     
  63.     shellcode = malloc(strlen((char *) injected_shellcode) +
  64.                strlen(mesg) + 4);
  65.     strcpy(shellcode, (char *) injected_shellcode); /* Get message and   */
  66.     strcat(shellcode, (char *) mesg);    /* code in shellcode */
  67.     
  68.     printf("Mesg : trying to launch shellcode on process %d\n", pid);
  69.  
  70.     sleep(1);
  71.     Tracer(pid);                 /* Call the tracer function */
  72.     usleep(1);
  73.     kill(pid, 9);                   /* Kill the process, Optional */
  74.     wait(NULL);            
  75.     return 0;
  76. }
  77.